package com.sen.pro.switches.core.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * 所有自定义拦截器基类
 * 
 * @author tian qingsen
 */
public abstract class BaseInterceptor implements Interceptor {

	private static final long serialVersionUID = 8133445387668674273L;

	protected static final String TIMEOUT_TYPE = "timeount";
	protected static final String EXCEPTION_TYPE = "exception";

	public void destroy() {
		// empty!
	}

	public void init() {
		// empty!
	}

	protected boolean isAjaxRequest() {
		HttpServletRequest request = ServletActionContext.getRequest();
		return "XMLHttpRequest".equalsIgnoreCase(request.getHeader("x-requested-with"));
	}

	protected void setErrorTipMessage(String errorType, String errorMessage) {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.reset();
		response.setContentType("text/html;charset=utf-8");
		response.setHeader("Error-Information", "{errorType:\"" + errorType + "\",message:\"" + errorMessage + "\"}");
		response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
	}
}
